package club.banyuan.http;

import club.banyuan.request.Request;
import club.banyuan.service.UserService;
import club.banyuan.util.Constant;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Stream;

/**
 * @author edz
 * @version 1.0
 * @date 2020/12/18 下午5:27
 */
public class HttpServer {
    public static void main(String[] args) {
        System.out.println("-------服务器启动-------");
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(Constant.PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }

//        Socket socket = new Socket();
        UserService us = new UserService();

        while (true) {
            try (Socket socket = serverSocket.accept();
                 //读取套接字获取到的字节输入流，封装成缓冲字符输入流
                 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                 //用字节流输出，封装成缓冲字节输出流
//               BufferedOutputStream dos = new BufferedOutputStream(socket.getOutputStream());
                 //字节输出流用来后面写数据
                 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()))
            {
                //new对象的时候同时操作一些东西，写在Request类里
                Request request = new Request(br);
//               System.out.println(request.getUrl());
                String url = request.getUrl();
                //.后缀的都是静态页面，放一起处理
                if (url.contains(".")) {
//                    System.out.println("接到页面请求" + url);
                    File file = new File(Constant.PATH, url);
                    if (!file.exists()) {
                        file = new File(Constant.PATH, "/404.html");
                    }
                    dos.write("HTTP/1.1 200 OK\n".getBytes());
                    dos.write(("Content-Length" + file.length() + "\n").getBytes());
                    if (url.endsWith(".html")) {
                        dos.write("Content-Type: text/html; charset=utf-8;\n".getBytes());
                    } else if (url.endsWith(".css")) {
                        dos.write("Content-Type: text/css; charset=utf-8;\n".getBytes());
                    }
                    dos.write("\n".getBytes());
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                    byte[] bytes = new byte[1024];
                    int len;
                    while ((len = bis.read(bytes)) != -1) {
                        dos.write(bytes, 0, len);
                    }
                    bis.close();
                }
                //实际操作，比如登录
                else {
                    System.out.println("接到业务请求" + url);
                    String data = request.getData();
                    System.out.println("数据：" + data);
                    switch (url) {
                        case "/server/user/login":
                            us.login(data, dos);
                            break;
                        case "/server/user/list":
                            us.getUserList(data,dos);
                            break;
                        case"/server/user/add":
                            us.addUser(data,dos);
                            break;
                        case"/server/user/get":
                            us.getUserById(data,dos);
                            break;
                        case"/server/user/update":
                            us.updateUser(data,dos);
                            break;
                        case"/server/user/delete":
                            us.deleteUser(data,dos);
                            break;
                        case "/server/user/getInfo":
                            us.getUserInfo(data,dos);
                            break;
                        case"/server/user/quit":
                            us.userQuit(data,dos);
                            break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
